<%@ page contentType="text/html" pageEncoding="UTF-8" %>
 <%@ taglib prefix="portlet" uri="http://java.sun.com/portlet_2_0" %>
<%@ taglib uri="http://liferay.com/tld/theme" prefix="liferay-theme" %>
<%@ taglib uri="http://liferay.com/tld/portlet" prefix="liferay-portlet"%>
<%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %>
<%@ taglib uri="http://liferay.com/tld/util" prefix="liferay-util" %>

<%@ page import="com.liferay.portal.theme.ThemeDisplay" %>
<%@ page import="com.liferay.portal.model.Group" %>

<portlet:defineObjects />
<liferay-theme:defineObjects/>
<portlet:resourceURL var='listCategoryToSelect' id='listCategoryToSelect' />
<portlet:resourceURL var='editCategory' id='editCategory' />

<!-- layui -->
<link href="<%=themeDisplay.getPathThemeJavaScript()%>/layui/css/layui.css" rel="stylesheet">
    
<form class="layui-form"  style="margin-top: 10px;" id="editForm">
<input name = "id" id="id" type="hidden" value="${categoryModel.id }"/>
  <div class="layui-form-item">
    <label class="layui-form-label">类别名称<span style="color: #E10601;">*</span></label>
    <div class="layui-input-block" style="width: 72%">
      <input name="name" lay-verify="required"  autocomplete="off"   class="layui-input" type="text"  value="${categoryModel.name }">
    </div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label">所属分类</label>
    <div class="layui-input-block" style="width: 72%">
       <select name="parentId" lay-filter="aihao" id="parentId" >
       <option value='0'>请选择</option>
      </select>
    </div>
  </div>
    <div class="layui-form-item">
    <label class="layui-form-label">状态</label>
    <div class="layui-input-block" style="width: 72%">
       <select name="status" lay-filter="aihao" id="status" >
         <option value="0">有效</option>
        <option value="1"  >无效</option>
       </select>
    </div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label">排序号<span style="color: #E10601;">*</span></label>
    <div class="layui-input-block" style="width: 72%">
      <input name="orderNo" lay-verify="required"   autocomplete="off" class="layui-input" type="text" value="${categoryModel.orderno }">
    </div>
  </div>
  <div class="layui-form-item">
    <div class="layui-input-block">
      <button class="layui-btn" id="affAdd"  type="button">立即提交</button>
      <button type="reset" class="layui-btn layui-btn-primary">重置</button>
    </div>
  </div>
</form>
 <!-- jQuery -->
    <script src="<%=themeDisplay.getPathThemeJavaScript()%>/gentelella/vendors/jquery/dist/jquery.min.js"></script>
<script src="http://static.runoob.com/assets/jquery-validation-1.14.0/dist/jquery.validate.min.js"></script>
     <script src="<%=themeDisplay.getPathThemeJavaScript()%>/layui/layui.js"></script>
 <!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->
<script>

$(function(){
	 $("#editForm").validate({
         rules: {
               name: {
                 required: true,
                 maxlength: 70
             },
             orderNo: {
                 required: true,
                 number: true
             } 
         },
         messages: {
        	 name: {
                 required: "请输入类别名称",
                 maxlength: "类别名称最大长度为70"
             },
             
             orderNo: {
                 required: "请输入排序号",
                 number: "必须为数字"
              } 
         },
         //重写showErrors
         showErrors: function (errorMap, errorList) {
             var msg = "";
             $.each(errorList, function (i, v) {
                 layer.msg(v.message, {icon: 5});
                 return false;
             });  
         },
         /* 失去焦点时不验证 */
         onfocusout: false
     });
      $("#affAdd").click(function(){
    	  var flag = $("#editForm").valid();  
          if (!flag) {  
                return;  
          }
      	  var d=$("#editForm").serialize();
			$.ajax({
				   type: "post",
					url : "${editCategory}",
					data :d,
					dataType : 'text',
 					success : function(response){
 						if(response=="success"){
							parent.layer.alert("编辑记录成功!", {
								 icon: 1,
	                    		  skin: 'layui-layer-molv' //样式类名
	                    		  ,closeBtn: 0
	                    		}, function(index1){
 									parent.layer.close(index1);
									var index = parent.layer.getFrameIndex(window.name);
									parent.layer.close(index); 
									parent.initDataPage();
	                    		});
	                     }else{
	                    	 parent.layer.alert("编辑出错!", {
	                    		 icon: 2,
								    skin: 'layui-layer-molv'
								    ,closeBtn: 0
								  });
	                     }
					},error  : function(response){
						 
					}
				  }); 
      });
	   $.ajax({  
	          url: '${listCategoryToSelect}',  
	          type: 'post',  
	          dataType: 'json',  
	          timeout: 1000,  
	          success:  function(tt) {  
	              var json = eval(tt); //数组 
	              var parentId = '${categoryModel.parentid}';
	                $.each(json, function (index, item) {
	                	 if(json[index].id == parentId){
	                		 $("#parentId").append("<option value='"+json[index].id+"'  selected='selected'>"+json[index].name+"</option>")
	                	 }else{
	                		 $("#parentId").append("<option value='"+json[index].id+"'>"+json[index].name+"</option>")
	                	 }
	              }); 
	                $("#status").val('${categoryModel.status}');
	                layui.use('form', function(){
	                	var form = layui.form();
	                	form.render('select');
	                	});
	          }  
	      });
	});

</script>
 